package com.bobby.service.impl;

import com.bobby.dao.BookDao;
import com.bobby.dao.impl.BookDaoImpl;
import com.bobby.pojo.Book;
import com.bobby.pojo.Page;
import com.bobby.service.BookService;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/8/14 16:04
 * @Created wenjunpei
 */
public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();

    @Override
    public int addBook(Book book) throws SQLException {
        return bookDao.addBook(book);
    }

    @Override
    public int deleteBookById(Integer id) throws SQLException {
        return bookDao.deleteBookById(id);
    }

    @Override
    public int updateBook(Book book) throws SQLException {
        return bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) throws SQLException {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() throws SQLException {
        return bookDao.queryBooks();
    }

    @Override
    public Page page(String pageNoStr, String pageSizeStr) throws SQLException {
        if(pageSizeStr == null) {
            pageSizeStr = String.valueOf(Page.PAGE_SIZE);
        }
        Long pageSize = Long.parseLong(pageSizeStr);
        Long pageTotalCount = bookDao.queryForPageTotalCount();
        Long pageTotal = pageTotalCount % pageSize == 0 ? (pageTotalCount / pageSize) : ( (pageTotalCount / pageSize) + 1 );
        if(pageNoStr == null ) {
            pageNoStr = "1";
        }
        Long pageNo = Long.parseLong(pageNoStr);
        if(pageNo < 1) {
            pageNo = 1L;
        }
        if(pageNo > pageTotal) {
            pageNo = pageTotal;
        }

        Long begin = (pageNo - 1) * pageSize;
        List<Book> items = bookDao.queryForItems(begin,pageSize);

        Page<Book> page = new Page<>();
        page.setItems(items);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setPageTotal(pageTotal);
        page.setPageTotalCount(pageTotalCount);
        return page;
    }

    @Override
    public Page pageByPrice(String pageNoStr, String pageSizeStr, String minStr, String maxStr) throws SQLException {
        if(minStr == null || "".equals(minStr)) {
            minStr = String.valueOf(Double.MIN_VALUE);
        }
        Double min = Double.parseDouble(minStr);
        if(maxStr == null || "".equals(maxStr)) {
            maxStr = String.valueOf(Double.MAX_VALUE);
        }
        Double max = Double.parseDouble(maxStr);

        if(pageSizeStr == null) {
            pageSizeStr = String.valueOf(Page.PAGE_SIZE);
        }
        Long pageSize = Long.parseLong(pageSizeStr);
        Long pageTotalCount = bookDao.queryForPageTotalCountByPrice(min,max);
        Long pageTotal = pageTotalCount % pageSize == 0 ? (pageTotalCount / pageSize) : ( (pageTotalCount / pageSize) + 1 );
        if(pageNoStr == null ) {
            pageNoStr = "1";
        }
        Long pageNo = Long.parseLong(pageNoStr);
        if(pageNo < 1) {
            pageNo = 1L;
        }
        if(pageNo > pageTotal) {
            pageNo = pageTotal;
        }

        Long begin = (pageNo - 1) * pageSize;
        List<Book> items = bookDao.queryForItemsByPrice(begin,pageSize,min,max);

        Page<Book> page = new Page<>();
        page.setItems(items);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setPageTotal(pageTotal);
        page.setPageTotalCount(pageTotalCount);
        return page;
    }
}
